﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data;
using MySql.Web;

namespace Artemis
{
    public partial class Systems : Form
    {
        public string connectionstring = null;
        public Systems(string Connectionstring)
        {
            InitializeComponent();
            connectionstring = Connectionstring;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sysName = null;
            sysName = cmbSys.Text;
            string sql = "select sysID from invsys where sysName = @sysName;";
            DataSet sysIDs = new DataSet();
            MySqlCommand query1 = new MySqlCommand(sql);
            query1.Parameters.Add(new MySqlParameter ("@sysName",sysName));
            sysIDs = EveCentral.getdata(query1, "sysID", connectionstring);
            if (sysIDs == null || sysIDs.Tables[0].Rows.Count == 0)
            {
                string sql1 = "select sysName from invsys where sysName like @sysName;";
                MySqlCommand query = new MySqlCommand(sql1);
                query.Parameters.Add(new MySqlParameter("@sysName", sysName + "%"));
                DataSet sysNames = new DataSet();
                sysNames = EveCentral.getdata(query, "sysName", connectionstring);
                List<object> sysnames = new List<object>();
                sysnames = Agents.refreshnoemptyline(sysNames, "sysName", "sysName");
                cmbSys.DataSource = sysnames;
                richTextBox1.Text = "";
                if (sysNames == null || sysNames.Tables[0].Rows.Count == 0)
                {
                    richTextBox1.Text = "错误的星系名称，请检查输入！";
                }
            }
            else
            {
                string sysID = null;
                sysID = sysIDs.Tables["sysID"].Rows[0]["sysID"].ToString();
                string sql2 = "select * from mapsolarsystems where solarSystemID = @sysID;";
                MySqlCommand sysfatt = new MySqlCommand(sql2);
                sysfatt.Parameters.Add(new MySqlParameter("@sysID", sysID));
                DataSet sysatt = new DataSet();
                sysatt = EveCentral.getdata(sysfatt, "systemAtt", connectionstring);
                if (sysatt != null && sysatt.Tables[0].Rows.Count != 0)
                {
                    string syssec = sysatt.Tables["systemAtt"].Rows[0]["security"].ToString();
                    string sysfacID = sysatt.Tables["systemAtt"].Rows[0]["factionID"].ToString();
                    string sqlfac = "select factionName from chrfactions where factionID = @factionID";
                    MySqlCommand queryfac = new MySqlCommand(sqlfac);
                    queryfac.Parameters.Add(new MySqlParameter("@factionID", sysfacID));
                    DataSet facname = new DataSet();
                    facname = EveCentral.getdata(queryfac, "factionName", connectionstring);
                    string sysfacname = null;
                    if (facname != null && facname.Tables[0].Rows.Count != 0)
                    {
                        sysfacname = facname.Tables["factionName"].Rows[0]["factionName"].ToString();
                    }
                    else
                    {
                        sysfacname = "无主权或玩家主权";
                    }
                    string sqlsysregion = "select regionName,ConstellationName from invsys where sysID = @sysID";
                    MySqlCommand queryregion = new MySqlCommand(sqlsysregion);
                    queryregion.Parameters.Add(new MySqlParameter("@sysID", sysID));
                    DataSet rgnname = new DataSet();
                    rgnname = EveCentral.getdata(queryregion, "region", connectionstring);
                    string sysrgn = rgnname.Tables["region"].Rows[0]["regionName"].ToString();
                    string syscon = rgnname.Tables["region"].Rows[0]["ConstellationName"].ToString();
                    richTextBox1.Text = "星系名称:" + sysName + "\n" + "所属星域:" + sysrgn + "\n" + "所在星座:" + syscon + "\n" + "主权归属:" + sysfacname + "\n" + "安全等级:" + syssec;
                }
            }
        }

        private void cmbSys_SelectedIndexChanged(object sender, EventArgs e)
        {
            button1_Click(sender,e);
        }
    }
}
